package com.guigu.qingcheng.service_user.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.qingcheng.pojo.user.TbCities;
import com.guigu.qingcheng.service_user.service.TbCitiesService;
import com.guigu.qingcheng.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 行政区域地州市信息表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-07-08
 */
@RestController
@RequestMapping("/service_user/tb-cities")
public class TbCitiesController {

    @Autowired
    private TbCitiesService citiesService;

    @PostMapping("/cities/findPage")
    public R findPage(@RequestParam("page") Integer page, @RequestParam("size") Integer size, @RequestBody(required = false) Map<String,Object> searchMap){
        Page<TbCities> page1=citiesService.findPage(searchMap, page, size);
        long total=page1.getTotal();
        List<TbCities> list=page1.getRecords();
        if(list.size()>0){
            return R.ok().data("total",total).data("rows",list);
        }
        return R.ok().data("list",page1);
    }

    /**
     * 根据ID查询数据
     * @param cityid
     * @return
     */
    @GetMapping("/cities/findById")
    public R findById(@RequestParam("cityid") String cityid){
        TbCities cities=citiesService.getById(cityid);
        if(cities==null){
            return R.error().data("cities",cities);
        }
        return R.ok().data("cities",cities);
    }

    /**
     * 增加数据
     * @param cities
     * @return
     */
    @PostMapping("/cities/add")
    public R add(@RequestBody TbCities cities){
        boolean save = citiesService.save(cities);
        if(save) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    /**
     * 修改数据
     * @param cities
     * @return
     */
    @PostMapping("/cities/update")
    public R update(@RequestBody TbCities cities){
        boolean flag = citiesService.updateById(cities);
        if(flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    /**
     * 删除数据
     * @param cityid
     * @return
     */
    @GetMapping("/cities/delete")
    public R delete(@RequestParam("cityid") String cityid){
        boolean flag=citiesService.removeById(cityid);
        if(flag){
            return R.ok();
        }else {
            return R.error();
        }
    }


}

